﻿Imports HTLibrary
Imports TNCBAL
Imports TNCDAL

Public Class ucConfig

    Private _objConfigController As New ConfigController
    Private _objConfig As tblConfig


    Private Sub ucConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridViewHelper.DataGridView_InitEvent(gridView)
        BindDataGridView()
        FormatDataGridView()
    End Sub

    Public Sub FormatDataGridView()
        CViewer.FormatDataGridView(gridView, New List(Of String)({"ID_Config"}))
    End Sub

    Public Sub BindDataGridView(Optional ByVal indexRow As Integer = -1)
        EnableButtons(False)
        'EnableButtons(False)
        Dim query As IQueryable(Of tblConfig) = _objConfigController.SelectCustom(txtNameSearch.Text, txtValueSearch.Text, txtDescriptionSearch.Text)
        Dim source As New BindingSource
        source.DataSource = query
        gridView.DataSource = source
        BindingNavigator1.BindingSource = source
        If indexRow > 0 Then
            'Set current row
            'gridViewContType.Rows(indexRow).Selected = True
            'gridViewContType.FirstDisplayedScrollingRowIndex = indexRow
            For Each col As DataGridViewColumn In gridView.Columns
                If col.Visible Then
                    gridView.CurrentCell = gridView.Rows(indexRow).Cells(col.Index)
                    Exit For
                End If
            Next
        End If

    End Sub


    ''' <summary>
    ''' Show/hide command buttons
    ''' </summary>
    ''' <param name="val"></param>
    ''' <remarks></remarks>
    Private Sub EnableButtons(ByVal val As Boolean)
        btnUpdate.Enabled = val
        btnCancel.Enabled = val

        'btnAdd.Enabled = Not val
        btnEdit.Enabled = Not val
        'btnDel.Enabled = Not val

        txtValue.ReadOnly = Not val
        txtName.ReadOnly = Not val
        txtDescription.ReadOnly = Not val
    End Sub

    Public Sub FillCustomerForm()
        'show result
        txtName.Text = _objConfig.ConfigName
        txtValue.Text = _objConfig.Value
        txtDescription.Text = _objConfig.Description
    End Sub

    Public Sub RefreshForm()
        'txtValue.Text = ""
        CViewer.ResetControls(Me.Controls)
    End Sub

    Public Sub GetInfoByForm()
        With _objConfig
            .ConfigName = txtName.Text
            .Value = txtValue.Text
            .Description = txtDescription.Text
        End With
    End Sub

    Private Sub gridViewConfig_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridView.SelectionChanged
        Try
            'Kiểm tra nếu đang cập nhật/thêm mới thì không hiển thị dữ liệu
            If btnUpdate.Enabled Then
                'MessageBox.Show("Đang chỉnh sửa dữ liệu. Không chọn được.")
                Return
            End If

            Dim id As Integer = gridView.CurrentRow.Cells("ID_Config").Value
            _objConfig = _objConfigController.SelectRecordById(id)
            '_CurrIndex = gridViewConfig.CurrentRow.Index

            'Fill company's fields
            FillCustomerForm()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        EnableButtons(True)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        GetInfoByForm()

        Dim indexRow As Integer = -1
        If _objConfig.ID_Config > 0 Then
            _objConfigController.Update(_objConfig)
            indexRow = gridView.CurrentRow.Index
        Else
            _objConfigController.Insert(_objConfig)
            indexRow = gridView.Rows.Count
        End If

        'Refresh gridview
        BindDataGridView()
        If gridView.Rows.Count > 0 Then
            'Set current row
            gridView.Rows(indexRow).Selected = True
        End If

        'Refresh buttons
        EnableButtons(False)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        EnableButtons(False)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindDataGridView(-1)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        txtValueSearch.Text = ""
        txtDescriptionSearch.Text = ""
        Me.btnSearch.PerformClick()
    End Sub
End Class
